﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GroupTool {

	private int MaxCard = 13;

	public void StartIt(){
		CheckCombination ();
	}

	public void CheckCombination(){
		
	}
		
	public List<List<Combination>> GroudCard( List<int> list ){

		List<List<Combination>> result = new List<List<Combination>> ();

		List<int> headType = new List<int>();
		List<int> bodyType = new List<int>();


		int count = 0;
		for(int i = 0 ; i < 20 ; i++){

			count++;
			List<int> l = new List<int> ( list.ToArray () );
			List<Combination> conbin = new List<Combination> ();
			Combination combincell2 = tryIt (headType,conbin,l);

			if (combincell2 == null || combincell2.Type == 0) {
				continue;
			}

			if ( count > 3) {
				if( headType.Count > 0 ){
					headType.RemoveAt(0);
				}
			}
			Combination combincell;
			tryIt (new List<int>(),conbin,l);


			List<int> fognter = new List<int> (l.ToArray());
			combincell = GetSub (l);
			if (combincell != null) {
				combincell.getValue ().Add (l [0]);
				conbin.Add (combincell);

			} else {
				combincell = new Combination ();
				combincell.Type = Combination.Chaos;
				combincell.setValue (fognter);
				conbin.Add (combincell);
			}

			bool flag = false;
			for( int b = 0 ; b < result.Count ; b++ ){
				if (result [b] [0].Type == conbin [0].Type && result [b] [1].Type == conbin [1].Type ) {
					flag = true;
				}
			}

			if(conbin [1].Type > conbin [0].Type){
				flag = true;
			}

			if ( !flag ) {
				result.Add (conbin);
			}
		}

		return result;
	}

	public Combination tryIt(List<int> Types,List<Combination> conbin,List<int> l){

		Combination combincell;
		if (Types.Contains (Combination.StraightColor) ) {

			combincell = GetStraghtColor (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Branch)) {

			combincell = GetBranch (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.Remove (l [random]);
				return combincell;
			}

		}

		if (!Types.Contains (Combination.Gourd) ) {

			combincell = GetGourds (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.SameColor) ) {

			combincell = GetSameColors (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Straight) ) {

			combincell = GetStraight (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Three)  ) {

			combincell = GetThrees (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.Remove(l[random]);

				random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.Remove (l[random]);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.DoubleSub)) {

			combincell = GetDoubleSub (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.Remove (l[random]);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Sub) ) {

			combincell = GetSub (l);

			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				List<int> lists = new List<int> ();
				for(int i = 0 ; i < l.Count ; i++){
					int value = getValue (l[i]);
					lists = new List<int> ();
					for(int s = 0 ; s < l.Count ; s++){
						int value2 = getValue (l[s]);
						if( value != value2 ){
							lists.Add (l [s]);
						}

						if( lists.Count == 2 ){
							lists.Add (l [i]);
							break;
						}
					}
				}

				for (int p = 0; p < lists.Count; p++) {
					l.Remove (lists [p]);
				}
				combincell.getValue ().AddRange (lists.ToArray ());
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Chaos) ) {

			combincell = new Combination ();
			combincell.Type = Combination.Chaos;
			List<int> newlist = new List<int> ();
			for( int i = 0 ; i < 5 ; i++ ){
				newlist.Add (l [i]);
			}
			combincell.setValue (newlist);
			conbin.Add (combincell);
			for( int i = 0 ; i < newlist.Count ; i++ ){
				l.Remove(newlist[i]);
			}
			return combincell;
		}

		return null;

	}

	//检查是否可以组成同花顺
	public List<int> TryStraightColor(List<int> value){

		value.Sort ();

		List<int> newList = CheckStraght (value,5,true);

		if( newList != null ){
			if (CheckColor (newList)) {
				return newList;
			} else {
				return null;
			}

		} else {
			return null;
		}

		return null;

	}

	//检查是否可以组成六对半
	public List<int> TrySixAndHalf(List<int> value){
		List<int> backUp = new List<int>(value.ToArray());
		value.Sort ();

		List<int> newList =  GetIronOrThreeOrDouble (value,2,true);
		List<int> newList2 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList3 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList4 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList5 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList6 = GetIronOrThreeOrDouble (value,2,true);

		if (newList != null && newList2 != null && newList3 != null && newList4 != null && newList5 != null && newList6 != null) {
			List<int> temp = new List<int> ();
			temp.AddRange (newList.ToArray ());
			temp.AddRange (newList2.ToArray ());
			temp.AddRange (newList3.ToArray ());
			temp.AddRange (newList4.ToArray ());
			temp.AddRange (newList5.ToArray ());
			temp.AddRange (newList6.ToArray ());
			return temp;
		} 

		value = backUp;
		return null;
	}

	//检查是否可以组成三顺子
	public List<List<int>> TryThreeStraight(List<int> value){
		List<int> backUp = new List<int>(value.ToArray());
		value.Sort ();

		List<int> newList = CheckStraght (value,5,true);
		List<int> newList2 = CheckStraght (value,5,true);
		List<int> newList3 = CheckStraght (value,3,true);

		if (newList != null && newList2 != null && newList3 != null) {

			List<List<int>> temp = new List<List<int>> ();
			temp.Add (newList);
			temp.Add (newList2);
			temp.Add (newList3);
			return temp;

		}
		value = backUp;
		return null;
	}

	//检查是否可以组成三同花顺
	public List<List<int>> TryThreeStraightAndSameColor(List<int> value){
		value.Sort ();

		List<int> newList = CheckStraght (value,5,true);
		List<int> newList2 = CheckStraght (value,5,true);
		List<int> newList3 = CheckStraght (value,3,true);

		if (newList != null && newList2 != null && newList3 != null) {
			if (CheckColor (newList) && CheckColor (newList2) && CheckColor (newList3)) {
				List<List<int>> temp = new List<List<int>> ();
				temp.Add (newList);
				temp.Add (newList2);
				temp.Add (newList3);
				return temp;
			} 
		} 
		return null;
	}

	//检查是否可以三同花
	public List<List<int>> TryThreeSameColor(List<int> value){
		List<int> temps = new List<int> (value.ToArray ());
		List<int> newList = GetSameColor (temps,5,true);
		List<int> newList2 = GetSameColor (temps,5,true);
		List<int> newList3 = GetSameColor (temps,3,true);

		if (newList != null && newList2 != null && newList3 != null) {

			List<List<int>> temp = new List<List<int>> ();

			temp.Add (newList);
			temp.Add (newList2);
			temp.Add (newList3);

			return temp;

		} 

		return null;
	}

	//尝试组一个顺子
	public List<int> CheckStraght( List<int> value,int length = 5,bool dele = false){
		value.Sort ();
		value.Reverse ();
		List<int> Straght = new List<int> ();

		for(int i = 0 ; i < value.Count ; i++ ){

			Straght = new List<int> ();

			if ( value [i] > 9 && value [i] < 14 ) {
				continue;
			}

			if ( value [i] > 22 && value [i] < 27 ) {
				continue;
			}

			if ( value [i] > 35 && value [i] < 40 ) {
				continue;
			}

			if ( value [i] > 48 ) {
				continue;
			}

			Straght.Add (value [i]);

			for( int f = 1 ; f < value.Count ; f++ ){

				if ( Straght.Count >= 5 ) {
					break;
				}

				if (value.Contains (value [i] + f)) {
					Straght.Add (value [i] + f);
					continue;
				}

				if (value.Contains (value[i] + MaxCard + f)  ) {
					Straght.Add (value[i] + MaxCard + f);
					continue;
				}

				if (value.Contains (value[i] + 2 * MaxCard + f) ) {
					Straght.Add (value[i] + 2 * MaxCard + f);
					continue;
				}

				if (value.Contains (value[i] + 3 *  MaxCard + f)) {
					Straght.Add (value[i] + 3 * MaxCard + f);
					continue;
				}
				break;
			}

			if(Straght.Count == length){
				if ( dele ) {
					for( int t = 0 ; t < Straght.Count ; t++ ){
						value.Remove (Straght[t]);
					}
				}
				return Straght;
			}
		}

		return null;
	}

	//检查是否同花
	public bool CheckColor( List<int> value){

		value.Sort ();

		int now = value [0];

		bool flag = true;

		if( value[0] > 0 && value[0] < 14 ){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] > 0 && value [i] < 14) {

				} else {
					return false;
				}
			}
		}

		if( value[0] >= 14 && value[0] < 27 ){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] >= 14 && value [i] < 27) {

				} else {
					return false;
				}
			}
		}

		if( value[0] >= 27 && value[0] < 40 ){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] >= 27 && value [i] < 40) {

				} else {
					return false;
				}
			}
		}


		if(value[0] >= 40 && value[0] < 53){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] >= 40 && value [i] < 53) {

				} else {
					return false;
				}
			}
		}

		return flag;
	}
		
	//获取一个同花
	public List<int> GetSameColor( List<int> value ,int length=5,bool dele = false){

		value.Sort ();

		List<int> list_1 = new List<int> ();
		List<int> list_2 = new List<int> ();
		List<int> list_3 = new List<int> ();
		List<int> list_4 = new List<int> ();

		for( int i = 0 ; i < value.Count ; i++ ){
			if( value[i] > 0 && value[i] < 14 ){
				list_1.Add (value[i]);
			}

			if( value[i] >= 14 && value[i] < 27 ){
				list_2.Add (value[i]);
			}

			if( value[i] >= 27 && value[i] < 40 ){
				list_3.Add (value[i]);
			}


			if( value[i] >= 40 && value[i] < 53){
				list_4.Add (value[i]);
			}

			if( list_1.Count == length ){
				DeleteListItem (value, list_1);
				return list_1;
			}

			if( list_2.Count == length ){
				DeleteListItem (value, list_2);
				return list_2;
			}

			if( list_3.Count == length ){
				DeleteListItem (value, list_3);
				return list_3;
			}

			if( list_4.Count == length ){
				DeleteListItem (value, list_4);
				return list_4;
			}
		}

		return null;
	}

	public void DeleteListItem(List<int> value,List<int> target){
		for( int i = 0 ; i < target.Count ; i++ ){
			value.Remove (target [i]);
		}
	}

	//获取一个铁支 或者三条 或者 对子
	public List<int> GetIronOrThreeOrDouble( List<int> value,int length = 4,bool dele = false){
		value.Sort ();
		List<int> temp = new List<int> ();

		for( int i = 0 ; i < value.Count ; i++ ){

			temp = new List<int> ();
			temp.Add (value [i]);

			int zjh = getValue (value [i]);

			for(int f = 0 ; f < value.Count ; f++){

				if(value[f] != value[i]){

					int zjh2 = getValue (value[f]);

					if(zjh == zjh2){
						temp.Add (value [f]);
					}
				}
			
				if( temp.Count == length ){
					if(dele){
						DeleteListItem (value, temp);
					}
					return temp;
				}
			}
		}

		return null;
	}

	//获取一个葫芦
	public List<int> GetGourd( List<int> value){

		List<int> three = GetIronOrThreeOrDouble (value, 3);
		List<int> Double =  GetIronOrThreeOrDouble (value, 2);
		if(three != null && Double != null){
			List<int> result = new List<int> ();
			for(int i = 0 ; i < three.Count ; i++ ){
				result.Add (three [i]);
			}

			for(int i = 0 ; i < Double.Count ; i++ ){
				result.Add (Double [i]);
			}
			return result;
		}
		return null;
	}

	public Combination GetStraghtColor(List<int> l){

		List<int> Straight = CheckStraght (l);
		if( Straight != null ){

			if (CheckColor (Straight)) {

				Combination combins = new Combination ();
				combins.Type = Combination.StraightColor;
				combins.setValue (Straight);
				return combins;
			}
		}

		return null;
	}

	public Combination GetBranch( List<int> l ){

		List<int> Branch = GetIronOrThreeOrDouble (l, 4, true);

		if ( Branch != null ) {

			Combination combins = new Combination ();
			combins.Type = Combination.Branch;
			combins.setValue (Branch);
			return combins;

		}

		return null;
	}

	public Combination GetGourds( List<int> l ){

		List<int> Gourdhead = GetIronOrThreeOrDouble (l,3,true);

		if (Gourdhead != null) {

			List<int> Gourdsfix = GetIronOrThreeOrDouble (l, 2, true);

			if (Gourdsfix != null) {

				Combination combins = new Combination ();
				combins.Type = Combination.Gourd;
				combins.setValue (Gourdhead);
				combins.getValue ().AddRange (Gourdsfix.ToArray ());
				return combins;

			} else {
				l.AddRange (Gourdhead.ToArray ());
			}
		}

		return null;
	}

	public Combination GetSameColors( List<int> l ){
		List<int> SameColor = GetSameColor (l,5,true);
		if ( SameColor != null ) {
			Combination combins = new Combination ();
			combins.Type = Combination.SameColor;
			combins.setValue (SameColor);
			return combins;
		}
		return null;
	}

	public Combination GetStraight( List<int> l ){

		List<int> Straght = CheckStraght (l,5,true);

		if ( Straght != null ) {
			Combination combins = new Combination ();
			combins.Type = Combination.Straight;
			combins.setValue (Straght);
			return combins;
		}

		return null;
	}

	public Combination GetThree( List<int> l ){

		List<int> Three = GetIronOrThreeOrDouble (l, 3, true);

		if (Three != null) {

			Combination combins = new Combination ();
			combins.Type = Combination.Three;
			combins.setValue (Three);
			return combins;
		}

		return null;
	}

	public Combination GetThrees( List<int> l ){

		List<int> Three = GetIronOrThreeOrDouble (l, 3, true);

		if (Three != null) {

			Combination combins = new Combination ();
			combins.Type = Combination.Three;
			combins.setValue (Three);
			return combins;
		}

		return null;
	}

	public Combination GetDoubleSub( List<int> l ){

		List<int> headSub = GetIronOrThreeOrDouble (l, 2, true);

		if (headSub != null) {

			List<int> backSub = GetIronOrThreeOrDouble (l, 2, true);

			if (backSub != null) {

				Combination combins = new Combination ();
				combins.Type = Combination.DoubleSub;
				headSub.AddRange (backSub.ToArray ());
				combins.setValue (headSub);
				return combins;

			} else {
				l.AddRange (headSub.ToArray ());
			}
		}
		return null;
	}

	public Combination GetSub( List<int> l ){

		List<int> headSub = GetIronOrThreeOrDouble (l, 2, true);

		if (headSub != null) {

			Combination combins = new Combination ();
			combins.Type = Combination.Sub;
			combins.setValue (headSub);
			return combins;
		}
		return null;
	}

	public int getValue(int cardValue){
		int Value = 0;
		if (cardValue > 0 && cardValue < 14) {
			Value = cardValue;
		} else if (cardValue >= 14 && cardValue < 27) {
			Value = cardValue - 13;
		} else if(cardValue >= 27 && cardValue < 40){
			Value = cardValue - 26;
		}else if(cardValue >= 40 && cardValue < 53){
			Value = cardValue - 39;
		}
		return Value;
	}

	void Update(){

	}
}
